package com.shen.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shen.eduservice.entity.EduCourse;
import com.shen.eduservice.entity.EduCourseDescription;
import com.shen.eduservice.entity.EduTeacher;
import com.shen.eduservice.entity.frontvo.CourseFrontVo;
import com.shen.eduservice.entity.vo.CourcePublishVo;
import com.shen.eduservice.entity.vo.CourseInfoVo;
import com.shen.eduservice.entity.vo.CourseWebVo;
import com.shen.eduservice.mapper.EduCourseMapper;
import com.shen.eduservice.service.EduChapterService;
import com.shen.eduservice.service.EduCourseDescriptionService;
import com.shen.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shen.eduservice.service.EduVideoService;
import com.shen.exceptionHandler.GuliException;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduCourseService eduCourseService;
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //添加课程信息 讲courseInfoVo 转化为 eduCourse
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //System.out.println(courseInfoVo);
        //System.out.println("educouse");
        System.out.println("eduCourse"+eduCourse);
        int  insert=baseMapper.insert(eduCourse);
        if(insert<=0){
            throw  new GuliException(20001,"添加课程信息失败");
        }
        String id=eduCourse.getId();
        //像课程简介表 添加数据 -eduservice desprition 自动注入
        EduCourseDescription description=new EduCourseDescription();
        //设置描述的id是我们课程的id
        description.setId(id);
        description.setDescription(courseInfoVo.getDescription());

        eduCourseDescriptionService.save(description);

        return id;
    }
        //根据课程id得到课程信息
    @Override
    public CourseInfoVo getCourceInfo(String courseID) {
        //先查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseID);
        //在查询描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseID);
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(description.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourceInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        System.out.println("before Update ---------------------------------");
        System.out.println("edu couese-----------------------------");
        System.out.println(eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription description=new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        boolean update1 = eduCourseDescriptionService.updateById(description);
        if(update==0){
            throw new GuliException(20001,"修改课程信息失败");
        }
    }

    @Override
    public CourcePublishVo PublishCourseInfo(String id) {
        //调用自己写的mapper 要用basemapper
        CourcePublishVo publishCourceInfo = baseMapper.getPublishCourceInfo(id);
        return publishCourceInfo;
    }

    @Override
    public boolean removeCourse(String id) {
        //删除小节
        boolean flag;
        eduVideoService.removeVideoByCourseId(id);
        //删除章节
        eduChapterService.removeByCourseId(id);
        // 删除描述
        eduCourseDescriptionService.removeById(id);
        //删除课程
        int count = baseMapper.deleteById(id);
        if(count==0){
            flag=false;
            throw  new GuliException(20001,"删除失败");
        }
        else {
            flag= true;
        }

        return flag;

    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> queryWrapper=new QueryWrapper<>();
        //判断条件值是否为空
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            queryWrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
            queryWrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            queryWrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//最新
            queryWrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//最新
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(coursePage, queryWrapper);

        long current = coursePage.getCurrent();
        long pages = coursePage.getPages();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();
        List<EduCourse> records;
        records = coursePage.getRecords();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String id) {
        return baseMapper.getBaseCourseInfo(id);
    }
}
